package com.jason.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jason.system.common.util.Pager;
import com.jason.system.entity.Fine;
import com.jason.system.entity.Reward;
import com.jason.system.entity.Student;
import com.jason.system.mapper.FineMapper;
import com.jason.system.mapper.StudentMapper;
import com.jason.system.service.FineService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author jie
 * @version 1.0
 * success come from self-discipline
 * @date 2021/4/24 16:39
 */
@Service
public class FineServiceImpl implements FineService {

  @Resource
  private FineMapper fineMapper;

  @Resource
  private StudentMapper studentMapper;

  @Override
  public Pager<Fine> getAllFine(Integer page, Integer size) {
    PageHelper.startPage(page,size);
    List<Fine> fineList = fineMapper.getAllFine();
    for (Fine f :fineList){
      //获取学生sid的String集合，以逗号分割
      String sIds = f.getSIds();
      //将ids的String转化成List<Integer>集合
      List<Integer> sIdList = Arrays.stream(sIds.split(",")).map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
      f.setStudents(studentMapper.getStudentByList(sIdList));
    }
    PageInfo<Fine> finePageInfo = new PageInfo<>(fineList);
    return new Pager<>(finePageInfo);
  }

  @Override
  public Pager<Fine> getAllFineByTime(Integer page, Integer size, Integer sIds, Date startTime, Date endTime) {
    PageHelper.startPage(page,size);
    List<Fine> fineList;
    if(startTime.equals(endTime)){
      fineList = fineMapper.getAllFineBysIds(sIds);
    }else {
      fineList = fineMapper.getAllFineByTime(sIds,startTime,endTime);
    }
    for (Fine f :fineList){
      //获取学生sid的String集合，以逗号分割
      String sIdArr = f.getSIds();
      //将ids的String转化成List<Integer>集合
      List<Integer> sIdList = Arrays.stream(sIdArr.split(",")).map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
      f.setStudents(studentMapper.getStudentByList(sIdList));
    }
    PageInfo<Fine> finePageInfo = new PageInfo<>(fineList);
    return new Pager<>(finePageInfo);
  }

  @Override
  public void insertFine(Fine fine) {
    fineMapper.insertFine(fine);
  }

  @Override
  public void updateFine(Fine fine) {
    fineMapper.updateFine(fine);
  }

  @Override
  public boolean deleteFine(Integer fId) {
    if (fId==null){
      return false;
    }else {
      fineMapper.deleteFine(fId);
      return true;
    }
  }

  @Override
  public boolean deleteFines(String fIds) {
    if (fIds==null||"".equals(fIds)){
      return false;
    }else {
      List<Integer> fIdList = Arrays.stream(fIds.split(",")).map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
      fineMapper.deleteFines(fIdList);
      return true;
    }
  }

  @Override
  public List<Reward> getAllStuReward() {
    List<Fine> fineList = fineMapper.getAllFine();
    Map<String,Reward> stringRewardMap = new HashMap<>(8);
    List<Reward> rewardList = new ArrayList<>();
    for (Fine f : fineList){
      //获取学生sid的String集合，以逗号分割
      String sIds = f.getSIds();
      //将ids的String转化成List<Integer>集合
      List<Integer> sIdList = Arrays.stream(sIds.split(",")).map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
      List<Student> studentList = studentMapper.getStudentByList(sIdList);
      for (Student s : studentList){
        if (!stringRewardMap.containsKey(s.getStuName())){
          Reward r = new Reward();
          r.setStuName(s.getStuName());
          //类别为1，表示奖励
          if (f.getCategory()==1){
            r.setTotalMoney(f.getMoney());
          }else {
            r.setTotalMoney(-f.getMoney());
          }
          rewardList.add(r);
          stringRewardMap.put(s.getStuName(),r);
        }else {
          Reward r = stringRewardMap.get(s.getStuName());
          if (f.getCategory()==1){
            r.setTotalMoney(r.getTotalMoney()+f.getMoney());
          }else {
            r.setTotalMoney(r.getTotalMoney()-f.getMoney());
          }
        }
      }
    }
    return rewardList;
  }
}
